package A_review.review9_4;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Class216 {

    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    int curSum = 0;

    int find(int k, int n) {
        int count = 0;

        backing(1, 9, k, n, 0);

        return count;
    }
void backing(int start, int n, int k, int sum, int curSum) {
        if (curSum > sum || start > sum) {
            return;
        }
        if (path.size() == k && curSum == sum) {
            result.add(new ArrayList<>(path));
        }
        for (int i = start; i <= n; i++) {
            curSum += i;
            path.add(i);
            backing(i + 1, n, k, sum, curSum);
            path.remove(path.size() - 1);
            curSum -= i;

        }

    }

    public static void main(String[] args) {
        Class216 class216 = new Class216();
        class216.find(3, 9);

        for (List list : class216.result) {
            Iterator iterator = list.iterator();
            while (iterator.hasNext()) {
                System.out.print(iterator.next() + " ");
            }
            System.out.println();
        }
}

}
